<template>
  <div class="app-container">

    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="员工名称" prop="shopUserName">
        <el-input v-model="queryParams.shopUserName" placeholder="请输入员工名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="商户名称" prop="tenantId">
        <el-select v-model="queryParams.tenantId" @change="chooseTenant" filterable clearable placeholder="请输入商户名称" @keyup.enter.native="handleQuery">
          <el-option v-for="tenant in tenants" :key="parseInt(tenant.id)" :label="tenant.name" :value="parseInt(tenant.id)" />
        </el-select>
      </el-form-item>
      <el-form-item label="分公司名称" prop="subTenantId">
        <el-select v-model="queryParams.subTenantId" @change="chooseSubTenant" filterable clearable placeholder="请输入分公司名称" @keyup.enter.native="handleQuery">
          <el-option v-for="subTenant in subTenants" :key="parseInt(subTenant.id)" :label="subTenant.name" :value="parseInt(subTenant.id)" />
        </el-select>
      </el-form-item>
      <el-form-item label="门店名称" prop="shopId">
        <el-select v-model="queryParams.shopId" filterable clearable placeholder="请输入门店名称" @keyup.enter.native="handleQuery">
          <el-option v-for="shop in shops" :key="parseInt(shop.id)" :label="shop.name" :value="parseInt(shop.id)" />
        </el-select>
      </el-form-item>
      <el-form-item label="提现类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择提现类型" clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.order_type)" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="提现时间" prop="createTime">
        <el-date-picker v-model="queryParams.createTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" :default-time="['00:00:00', '23:59:59']" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 列表 -->
    <el-table v-loading="loading" :data="list">
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="250">
        <template v-slot="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handlePass(scope.row)" v-hasPermi="['pekingfish:app-user:update']">同意
          </el-button>
          <el-button @click="handleRemark(scope.row)" icon="el-icon-edit" type="text" size="small" v-hasPermi="['pekingfish:app-user:remark']">
            备注
          </el-button>
          <el-button @click="handleDetail(scope.row)" type="text" size="small" v-hasPermi="['pekingfish:app-user:view']">
            收款账户
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="提现类型" align="center" prop="type" />
      <el-table-column label="商户名称" align="center" prop="tenantName" width="160" />
      <el-table-column label="分公司名称" align="center" prop="subTenantName" width="160" />
      <el-table-column label="门店名称" align="center" prop="shopName" width="160" />
      <el-table-column label="员工名称" align="center" prop="shopUserName" />
      <el-table-column label="提现金额" align="center" prop="benefit" />
      <el-table-column label="提现时间" align="center" prop="updateTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核人" align="center" prop="auditUserName" />
      <el-table-column label="审核备注" align="center" prop="remark" />

    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="title" :visible.sync="open" width="500px" v-dialogDrag append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" v-if="form.type!='订单钱包'">
        <el-form-item label="姓名" prop="realName">
          <el-input readonly v-model="form.realName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="账号" prop="account">
          <el-input readonly v-model="form.account" placeholder="请输入账号" />
        </el-form-item>
        <el-form-item label="开户行" prop="bank">
          <el-input readonly v-model="form.bank" placeholder="请输入开户行" />
        </el-form-item>
        <el-form-item label="收款码" prop="idCardImage">
          <div style="display:flex">
            <div style="margin-right:15px;margin-bottom:15px;" v-for="(item,index) in form.idCardImage" :key="index">
              <ImagePreview :src="item" :width="80" :height="80" />
            </div>
          </div>
        </el-form-item>
      </el-form>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" v-if="form.type=='订单钱包'">
        <el-form-item label="姓名" prop="warName">
          <el-input readonly v-model="form.warName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="账号" prop="warAccount">
          <el-input readonly v-model="form.warAccount" placeholder="请输入账号" />
        </el-form-item>
        <el-form-item label="开户行" prop="warBank">
          <el-input readonly v-model="form.warBank" placeholder="请输入开户行" />
        </el-form-item>
        <el-form-item label="收款码" prop="warIdCardImage">
          <div style="display:flex">
            <div style="margin-right:15px;margin-bottom:15px;" v-for="(item,index) in form.warIdCardImage" :key="index">
              <ImagePreview :src="item" :width="80" :height="80" />
            </div>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <!-- <el-button type="primary" @click="submitForm">确 定</el-button> -->
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  createAppShareBenefitDetail,
  updateAppShareBenefitDetail,
  deleteAppShareBenefitDetail,
  getAppShareBenefitDetail,
  getAppShareBenefitDetailPage,
  exportAppShareBenefitDetailExcel,
} from "@/api/pekingfish/appShareBenefitDetail";
import ImagePreview from "@/components/ImagePreview";
import { getBusinessList } from "@/api/merchantManagement/business";
import { getBranchCompanyByTenantId } from "@/api/merchantManagement/branchCompany";
import { getStoreBySuTenantId } from "@/api/merchantManagement/store";

import {
  withdrawAuditPage,
  passWithdraw,
  updateRemark,
} from "@/api/pekingfish/embodyAudit";

export default {
  name: "embodyAudit",
  components: { ImagePreview },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 分润明细列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        shareBenefit: null,
        waterBillType: null,
        detailModeRemark: null,
        detailType: null,
        detailMode: null,
        shopUserId: null,
        outOrderNo: null,
        shareBenefitMode: null,
        createTime: [],
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      // 商户名称
      tenants: [],
      // 分公司名称
      subTenants: [],
      //门店名称
      shops: [],
    };
  },
  created() {
    this.getList();

    // 商户名称
    getBusinessList().then((response) => {
      this.tenants = response.data.list;
    });
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      withdrawAuditPage(this.queryParams).then((response) => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        realName: undefined,
        account: undefined,
        bank: undefined,
        idCardImage: undefined,
        warName: undefined,
        warAccount: undefined,
        warBank: undefined,
        warIdCardImage: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加分润明细";
    },
    /** 修改按钮操作 */
    handleDetail(row) {
      this.reset();
      let data = JSON.parse(JSON.stringify(row));
      this.title = "收款账号 # " + data.realName;
      this.form = data;
      this.form.idCardImage =
        data.idCardImage && typeof data.idCardImage == "string"
          ? JSON.parse(data.idCardImage)
          : data.idCardImage;
      this.form.warIdCardImage =
        data.warIdCardImage && typeof data.warIdCardImage == "string"
          ? JSON.parse(data.warIdCardImage)
          : data.warIdCardImage;
      this.open = true;
    },
    // 通过
    handlePass(row) {
      const id = row.id;
      const type = row.type;
      this.$modal
        .confirm("是否确认同意？")
        .then(function () {
          return passWithdraw({
            id,
            type,
          });
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("操作成功");
        })
        .catch(() => {});
    },
    handleRemark(row) {
      this.$prompt(" ", "备注", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        inputPlaceholder: "请输入备注",
        inputType: "textarea",
      })
        .then(({ value }) => {
          updateRemark({
            remark: value,
            id: row.id,
            type: row.type,
          }).then((rs) => {
            this.getList();
            this.$modal.msgSuccess("操作成功");
          });
        })
        .catch(() => {});
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (!valid) {
          return;
        }
        // 修改的提交
        if (this.form.id != null) {
          updateAppShareBenefitDetail(this.form).then((response) => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
          return;
        }
        // 添加的提交
        createAppShareBenefitDetail(this.form).then((response) => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      });
    },
    chooseTenant(tenantId) {
      console.log(tenantId + "获取的id");
      // 分公司名称
      getBranchCompanyByTenantId(tenantId).then((response) => {
        this.subTenants = response.data;
      });
    },

    chooseSubTenant(subTenantId) {
      console.log(subTenantId + "获取的id");
      // 门店名称
      getStoreBySuTenantId(subTenantId).then((response) => {
        this.shops = response.data;
      });
    },
  },
};
</script>
